package middleware

import (
	"fmt"
	api "go-gw-scaffold/api/limiter"
	"log"
	"time"

	"github.com/gin-gonic/gin"
)

var RequestLimit = api.NewRequestLimiter(10*time.Second, 5)

func RequestLimiterMiddleware() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		if RequestLimit.IsAvailable() {
			RequestLimit.Increase()
			fmt.Println(RequestLimit.RequestCount)
		} else {
			log.Fatal("request limiting")
			ctx.Abort()
			return
		}
		ctx.Next()
	}
}
